//
//  YYT_RegisterViewController.m
//  AudioVisualFeast
//
//  Created by dllo on 16/2/24.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "QL_RegisterViewController.h"

@interface QL_RegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *dengluZ;
@property (weak, nonatomic) IBOutlet UITextField *mimaZ;
@property (weak, nonatomic) IBOutlet UITextField *zaiciZ;
@property (weak, nonatomic) IBOutlet UITextField *youxiangZ;
@property (weak, nonatomic) IBOutlet UITextField *dianhua;
@property (weak, nonatomic) IBOutlet UITextField *yanzhengma;
@property (weak, nonatomic) IBOutlet UIImageView *denglu;
@property (weak, nonatomic) IBOutlet UIImageView *mima;
@property (weak, nonatomic) IBOutlet UIImageView *zaici;
@property (weak, nonatomic) IBOutlet UIImageView *youxiang;
@end

@implementation QL_RegisterViewController
- (IBAction)yanzhengma:(id)sender {
    
    [BmobSMS requestSMSCodeInBackgroundWithPhoneNumber:self.dianhua.text andTemplate:@"test" resultBlock:^(int number, NSError *error) {
        if (error) {
            NSLog(@"%@",error);
        } else {
            //获得smsID
            NSLog(@"sms ID：%d",number);
        }
    }];
}
- (IBAction)zhuce:(id)sender {
    
    BOOL aa = [_mimaZ.text isEqual: _zaiciZ.text];
    if(aa == 0 && _dengluZ.text.length != 0 && _mimaZ.text.length != 0 && _zaiciZ.text.length != 0 && _youxiangZ.text.length != 0)
        
    {
    UIAlertController *alertB = [UIAlertController alertControllerWithTitle:@"提示" message:@"两次密码输入不一致" preferredStyle:UIAlertControllerStyleActionSheet];
    [self presentViewController:alertB animated:YES completion:^{
    }];
    UIAlertAction *alertD = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
        //添加提示按钮
        
        [alertB addAction:alertD];
    }
    
    else if (aa != 0 && _dengluZ.text.length == 0 && _mimaZ.text.length != 0 && _zaiciZ.text.length != 0 && _youxiangZ.text.length != 0)
        
    {
        UIAlertController *alertE = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名不能为空" preferredStyle:UIAlertControllerStyleActionSheet];
        [self presentViewController:alertE animated:YES completion:^{
        }];
        UIAlertAction *alertF = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        //添加提示按钮
        [alertE addAction:alertF];
    }
    
    else if (aa != 0 && _dengluZ.text.length != 0 && _mimaZ.text.length == 0 && _zaiciZ.text.length != 0 && _youxiangZ.text.length != 0)
        
    {
        UIAlertController *alertG = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码不能为空" preferredStyle:UIAlertControllerStyleActionSheet];
        [self presentViewController:alertG animated:YES completion:^{
        }];
        UIAlertAction *alertH = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        //添加提示按钮
        [alertG addAction:alertH];
    }
    
    else if (aa != 0 && _dengluZ.text.length != 0 && _mimaZ.text.length != 0 && _zaiciZ.text.length == 0 && _youxiangZ.text.length != 0)
        
    {
        UIAlertController *alertI = [UIAlertController alertControllerWithTitle:@"提示" message:@"请重新出入密码" preferredStyle:UIAlertControllerStyleActionSheet];
        [self presentViewController:alertI animated:YES completion:^{
        }];
        UIAlertAction *alertJ = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        //添加提示按钮
        [alertI addAction:alertJ];
    }
    
    else if(aa != 0 && _dengluZ.text.length != 0 && _mimaZ.text.length != 0 && _zaiciZ.text.length != 0 && _youxiangZ.text.length == 0)
        
    {
        UIAlertController *alertK = [UIAlertController alertControllerWithTitle:@"提示" message:@"邮箱不能为空" preferredStyle:UIAlertControllerStyleActionSheet];
        [self presentViewController:alertK animated:YES completion:^{
        }];
        UIAlertAction *alertL = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        //添加提示按钮
        [alertK addAction:alertL];
    }
    
    else if (aa == 1 && _dengluZ.text.length != 0 && _mimaZ.text.length != 0 && _zaiciZ.text.length != 0 && _youxiangZ.text.length != 0)
        
    {
        //查找GameScore表
        BmobQuery   *bquery = [BmobQuery queryWithClassName:@"qingjiaowodazui"];
        [bquery getObjectInBackgroundWithId:nil block:^(BmobObject *object, NSError *error) {
            
            if (error)
            
            {
                
                
                
                BmobUser *bUser = [[BmobUser alloc] init];
                [bUser setUsername:self.dengluZ.text];
                [bUser setPassword:self.mimaZ.text];
                [bUser setEmail:self.youxiangZ.text];
                [bUser signUpInBackgroundWithBlock:^ (BOOL isSuccessful, NSError *error){
                    if (isSuccessful)
                    
                    {
                        NSLog(@"Sign up successfully");
                        [self dismissViewControllerAnimated:YES completion:nil];
                    }
                    
                    else
                        
                    {
                        NSString *errorInfo = [error.userInfo objectForKey:@"error"];
                        
                        if ([errorInfo rangeOfString:@"username"].location != NSNotFound)
                        
                        {
                            
                            UIAlertController *alertA = [UIAlertController alertControllerWithTitle:@"提示" message:@"这个用户名已经被占用" preferredStyle:UIAlertControllerStyleActionSheet];
                            [self presentViewController:alertA animated:YES completion:^{
                                
                                
                                
                            }];
                            UIAlertAction *alertC = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                                
                                
                            }];
                            //添加提示按钮
                            [alertA addAction:alertC];
                        }
                        
                        else if ([errorInfo rangeOfString:@"email"].location != NSNotFound)
                        
                        {
                            
                            UIAlertController *alertA = [UIAlertController alertControllerWithTitle:@"提示" message:@"这个邮箱已经被使用过" preferredStyle:UIAlertControllerStyleActionSheet];
                            [self presentViewController:alertA animated:YES completion:^{
                                
                                
                                
                            }];
                            UIAlertAction *alertC = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                                
                                
                            }];
                            //添加提示按钮
                            [alertA addAction:alertC];
                        }
                        
                        else
                            
                        {
                            
                            [BmobSMS verifySMSCodeInBackgroundWithPhoneNumber:self.dianhua.text andSMSCode:self.yanzhengma.text resultBlock:^(BOOL isSuccessful, NSError *error) {
                                if (isSuccessful)
                                
                                {
                                    NSLog(@"%@",@"验证成功，可执行用户请求的操作");
                            
                                    UIAlertController *alertA = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功" preferredStyle:UIAlertControllerStyleActionSheet];
                                    [[NSUserDefaults standardUserDefaults] setObject:@"yes" forKey:@"states"];
                                    [[NSUserDefaults standardUserDefaults] synchronize];
                                    [self.navigationController popViewControllerAnimated:YES];
                                    [self presentViewController:alertA animated:YES completion:^{
                                        
                                        
                                        
                                    }];
                                    UIAlertAction *alertC = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                                        [self.navigationController popToRootViewControllerAnimated:YES];
                                        
                                    }];
                                    //添加提示按钮
                                    [alertA addAction:alertC];
                                }
                                
                                else
                                
                                {
                                    NSLog(@"%@",error);
                                    UIAlertController *passCheckError = [UIAlertController alertControllerWithTitle:@"提示" message:@"验证出错,请重新验证" preferredStyle:UIAlertControllerStyleAlert];
                                    UIAlertAction *well = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                                        
                                    }];
                                    [passCheckError addAction:well];
                                    [self presentViewController:passCheckError animated:YES completion:^{
                                        
                                        
                                    }];
                                 }
                            }];
                        }
                    }
                }];
            }
        }];
    }
}

- (IBAction)quxiao:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBarHidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
